<template>
  <div class="app-container">
    <h3>新增商品</h3>
    <el-divider></el-divider>
    <div class="u-flex u-flex-wrap">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <h4>基础信息</h4>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品封面" prop="image">
          <Upload v-model="form.image" />
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
        <h4>销售信息</h4>
        <el-form-item label="商品价格" prop="price">
          <el-input
            v-model="form.price"
            placeholder="请输入商品价格"
          ></el-input>
        </el-form-item>
        <el-form-item label="上架方式" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">立即上架</el-radio>
            <el-radio :label="0">暂不上架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">提交</el-button>
          <el-button @click="$router.go(-1)">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { goodsDetail, saveProducts, updateGoods } from "@/api/user";
import Upload from "@/components/Upload/SingleImage3";
export default {
  components: { Upload },
  data() {
    return {
      list: [],
      loading: false,
      // currentPage: 1,
      queryData: {
        status: "", //	商品状态 0 下架 1 上架
        name: "",
      },
      pagination: {
        pageSize: 1,
        total: 1,
      },
      typeStatus: [
        { id: "", name: "不限" },
        { id: 0, name: "商品详情" },
        { id: 1, name: "H5" },
      ],
      form: {
        name: "",
        image: "",
        description: "",
        price: "",
        status: 0,
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        image: [{ required: true, message: "请上传封面", trigger: "change" }],
        description: [
          { required: true, message: "请输入商品描述", trigger: "blur" },
          { max: 500, message: "长度不超过500字符", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        status: [
          { required: true, message: "请选择上架方式", trigger: "change" },
        ],
      },
      goodsId: "",
    };
  },
  created() {
    const { params, query } = this.$route;
    if (query.goodsId) {
      this.goodsId = query.goodsId;
      this.goodsDetail(query.goodsId);
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.queryData.pageIndex + 1;
      },
      set() {},
    },
  },
  methods: {
    goodsDetail(goodsId) {
      goodsDetail(goodsId).then((res) => {
        this.form = res.data;
      });
    },
    hanleSearch() {
      this.queryData.pageIndex = 0;
      this.getList();
    },
    handleSizeChange(val) {
      // this.queryData.pageIndex = val - 1
      // this.getList()
    },
    handleCurrentChange(val) {
      this.queryData.pageIndex = val - 1;
      this.getList();
    },
    handleEdit(id) {
      this.$router.push({
        path: "/swiper/edit",
        query: {
          id: id,
        },
      });
    },
    handleDelete(id) {
      this.$confirm("确认要删除该轮播图么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.confirmDelete(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    confirmDelete(id) {
      bannerDelete({ id }).then((res) => {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.getList();
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid, b, c) => {
        if (valid) {
          if (this.goodsId) {
            this.form.goodsId = this.goodsId;
            updateGoods(this.form).then(() => {
              this.$router.push("/permission/page");
            });
            return;
          }
          saveProducts(this.form).then(() => {
            this.$router.push("/permission/page");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
</style>
